//
//  EditTPLVC.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2020/8/31.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "EditTPLVC.h"
#import "DriverAddCell.h"
#import "DriverAddInPutCell.h"

@interface EditTPLVC () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) TPLMainModel *model;

@end

@implementation EditTPLVC

-(TPLMainModel *)model {
    if (!_model) {
        _model = [TPLMainModel new];
    }
    return _model;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - 64 - BottomHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        
        UIButton *saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, ScreenW - 60, 44)];
        [saveBtn setBackgroundColor:Color_ThemeColor];
        saveBtn.layer.masksToBounds = YES;
        saveBtn.layer.cornerRadius = 6;
        [saveBtn setTitle:@"保存" forState:0];
        [saveBtn setTitleColor:HexColor(0xffffff) forState:0];
        saveBtn.titleLabel.font = BoldFontOfSize(16);
        [saveBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:saveBtn];
    }
    return _bottomView;
}

- (void)saveClick {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (self.model.name.length) {
        [dic setValue:self.model.name forKey:@"name"];
    }
    if (self.model.liaison.length) {
        [dic setValue:self.model.liaison forKey:@"liaison"];
    }
    if (self.model.tel.length) {
        [dic setValue:self.model.tel forKey:@"tel"];
    }
    if (self.model.url.length) {
        [dic setValue:self.model.url forKey:@"url"];
    }
    if (self.model.postcode.length) {
        [dic setValue:self.model.postcode forKey:@"postcode"];
    }
    if (self.model.fax.length) {
        [dic setValue:self.model.fax forKey:@"fax"];
    }
    if (self.model.remarks.length) {
        [dic setValue:self.model.remarks forKey:@"remarks"];
    }
    if (self.model.address.length) {
        [dic setValue:self.model.address forKey:@"address"];
    }
    [dic setValue:self.model.s_t_id forKey:@"s_t_id"];
    [HttpRequest PostWithApi:LogisticsSupplierEditTPLInfo params:dic progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"保存成功"];
            [self.rt_navigationController popViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshTPLList object:nil];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"编辑";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:LogisticsSupplierGetTPLInfo params:@{@"t_id":self.t_id} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.model = [TPLMainModel mj_objectWithKeyValues:responseObject];
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 7;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        DriverAddCell *cell = [DriverAddCell DriverAddCellWithTableView:tableView];
        if (indexPath.row == 0) {
            cell.titleText = @"企业名称";
            cell.showLine = YES;
            cell.canClick = NO;
            cell.isMust = NO;
            cell.canNOUserOperation = YES;
            cell.contentText = self.model.name;
//            __weak_self;
//            cell.cellBlock = ^(NSString * _Nonnull str) {
//                weakself.model.name = str;
//            };
        }else if (indexPath.row == 1) {
            cell.titleText = @"联系人";
            cell.showLine = YES;
            cell.canClick = NO;
            cell.isMust = NO;
            cell.contentText = self.model.liaison;
            __weak_self;
            cell.cellBlock = ^(NSString * _Nonnull str) {
                weakself.model.liaison = str;
            };
        }else if (indexPath.row == 2) {
            cell.titleText = @"联系方式";
            cell.showLine = YES;
            cell.canClick = NO;
            cell.isMust = NO;
            cell.contentText = self.model.tel;
            __weak_self;
            cell.cellBlock = ^(NSString * _Nonnull str) {
                weakself.model.tel = str;
            };
        }else if (indexPath.row == 3) {
            cell.titleText = @"企业地址";
            cell.showLine = YES;
            cell.canClick = NO;
            cell.isMust = NO;
            cell.contentText = self.model.address;
            __weak_self;
            cell.cellBlock = ^(NSString * _Nonnull str) {
                weakself.model.address = str;
            };
        }else if (indexPath.row == 4) {
            cell.titleText = @"网址";
            cell.showLine = YES;
            cell.canClick = NO;
            cell.isMust = NO;
            cell.contentText = self.model.url;
            __weak_self;
            cell.cellBlock = ^(NSString * _Nonnull str) {
                weakself.model.url = str;
            };
        }else if (indexPath.row == 5) {
            cell.titleText = @"邮编";
            cell.showLine = YES;
            cell.canClick = NO;
            cell.isMust = NO;
            cell.contentText = self.model.postcode;
            __weak_self;
            cell.cellBlock = ^(NSString * _Nonnull str) {
                weakself.model.postcode = str;
            };
        }else if (indexPath.row == 6) {
            cell.titleText = @"传真";
            cell.showLine = NO;
            cell.canClick = NO;
            cell.isMust = NO;
            cell.contentText = self.model.fax;
            __weak_self;
            cell.cellBlock = ^(NSString * _Nonnull str) {
                weakself.model.fax = str;
            };
        }
        return cell;
    }else {
        DriverAddInPutCell *cell = [DriverAddInPutCell DriverAddInPutCellWithTableView:tableView];
        __weak_self;
        cell.contentText = self.model.remarks;
        cell.remarkBlock = ^(NSString * _Nonnull str) {
            weakself.model.remarks = str;
        };
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        DriverAddCell *cell = [DriverAddCell DriverAddCellWithTableView:tableView];
        return cell.height;
    }
    DriverAddInPutCell *cell = [DriverAddInPutCell DriverAddInPutCellWithTableView:tableView];
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 2) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
        view.backgroundColor = Color_backgroundColor;
        return view;
    }
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return 10;
    }
    return 0;
}

@end
